<?php

include_once ('bn_path.php');

class bn_menuctrl
{
    public $name = 'menuctrl1';
    public $selected='';
    public $width = 200;
    public $height = 400;
    public $checkbox = false;
    public $check_multi = false;
    public $idCOL = 'id';
    public $pidCOL = 'pid';
    public $textCOL = 'text';
    public $urlCOL = 'url';
    public $describeCOL = 'descr';
    public $css_link;
    public $css_item;
    public $css_class;
    public $border_color = '';
    public $tree_plusimg = '../template/imgs/plus.png';
    public $tree_minusimg = '../template/imgs/minus.png';
    private $ds;
    private $select_style='';
    protected static $instance;

    function __construct($id = '', $pid = '', $text = '', $url = '', $describe = '')
    {
        if (!empty($id))
            $this->idCOL = $id;
        if (!empty($pid))
            $this->pidCOL = $pid;
        if (!empty($text))
            $this->textCOL = $text;
        if (!empty($url))
            $this->urlCOL = $url;
        if (!empty($describe))
            $this->describeCOL = $describe;
        $this->tree_plusimg = bn_Get_sitename() . root . '/template/imgs/plus.png';
        $this->tree_minusimg = bn_Get_sitename() . root . '/template/imgs/minus.png';
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function Treeview($ds, $pid = 0)
    {
        $out = '';
        $li = '';
        $ret = '';
        $cnt = count($ds);
        $out = '';
        for ($i = 0; $i < $cnt; $i++)
        {
            if ($ds[$i][$this->pidCOL] == $pid)
            {
                $titem = $ds[$i];
                if (empty($titem[$this->urlCOL]))
                    $titem[$this->urlCOL] = "javascript:void(0)";
                $check = '';
                if ($this->checkbox)
                    $check = "<input type='checkbox' name='tree_check[$i]' id='tree_check[]' />";
                if (isset($titem[$this->describeCOL]))
                    $descr = ' title="' . $titem[$this->describeCOL] . '"';
                if($this->selected==$titem[$this->idCOL])
                $link = "$check<a$this->css_link href='{$titem[$this->urlCOL]}' id-data='{$titem[$this->idCOL]}' style='background-color:lightblue;border-radius:5px;padding:1px 5px 2px 5px;'$descr>{$titem[$this->textCOL]}</a>";
                else
                $link = "$check<a$this->css_link href='{$titem[$this->urlCOL]}' id-data='{$titem[$this->idCOL]}'$descr>{$titem[$this->textCOL]}</a>";
                $ret = self::Treeview($ds, $titem[$this->idCOL]);
                //echo $link."<br /><br />";
                if (empty($ret))
                    $out .= "\n<li$this->css_item style='margin:0px 30px 0px 30px'>\n$link\n</li>";
                else
                    $out .= "\n<li>\n<div><img src='$this->tree_minusimg' width='30' height='30' id='treeimg'/>$link</div>\n<ul>\n$ret\n</ul>\n</li>";
            }
        }
        return $out;
    }


    public function treemenu($datasource = array())
    {
        $out = '';
        if (!empty($datasource))
        {
            if ($this->css_item != '')
                $this->css_item = " class='$this->css_item'";
            if ($this->css_link != '')
                $this->css_link = " class='$this->css_link'";
            if ($this->css_class != '')
                $this->css_class = " class='$this->css_class'";
            if ($this->width != '')
                $this->width = "width:" . $this->width . "px;";
            if ($this->height != '')
                $this->height = "height:" . $this->height . "px;";
            if ($this->border_color == '')
                $this->border_color = 'gray';

            if (!empty($this->idCOL) && !empty($this->pidCOL) && !empty($this->textCOL))
            {
                $tree='';
                $tree = self::Treeview($datasource, 0);

                $out .= "\n<div id='$this->name' name='$this->name' style='border:1px solid $this->border_color;overflow:scroll;$this->width$this->height'>
                \n<ul style='margin:8px 3px 0 0'>$tree</ul>
                \n<input type='hidden' name='" . $this->name . "_id' id='" . $this->name . "_id' value='$this->selected'/>
                \n</div>";
            } else
                return 2;
        }
        return $out;
    }
    
    public function command_script()
    {
        $multi = '';
        if ($this->checkbox == false)
        $multi = "$('#$this->name a').live('click',function(){
        if($(this).css('background-color')=='transparent')
        {
             $('#" . $this->name . "_id').val($(this).attr('id-data'));
             $('#$this->name a').css('background-color','');
             $(this).css({'background-color':'lightblue','border-radius':'3px','padding':'1px 5px 5px 5px'});
        }
        else
        {
        $('#" . $this->name . "_id').val(''); 
        $(this).css('background-color','');
        }
        });";
            
        $nomulti = '';
        if ($this->checkbox && !$this->check_multi)
        $nomulti = "$('#$this->name input:checkbox').live('click',function(){
        var ch=$(this).is(':checked');
        $('input:checkbox').removeAttr('checked');
        if(ch)
        $(this).attr('checked','true');
        });";
        
        return "
        <style scoped>
        #$this->name li
        {
          list-style:none;
          padding-top:5px;
          min-width:100px;
        }
        #$this->name a
        {
          text-decoration:none; 
        }
        #$this->name img
        {
         margin-top:-8px;   
        }
        #$this->name li ul
        {
             padding: 3px 35px 3px 30px;float:right;
             
        }
        </style>
        
        <script>
        
        $('#$this->name img').live('click',function(){
            var obj=$(this).parent().parent().find('ul:eq(0)');
            if(obj.is(':hidden'))
            {
                obj.show(500);
                $(this).attr('src','$this->tree_minusimg'); 
            }
            else
            {
                obj.hide(500);
                $(this).attr('src','$this->tree_plusimg');
            }
        });
        
        $multi
        
        $nomulti
        </script>";   
    }


    function navmenu($datasource = array(), $pid = 0)
    {
        $out = '';
        if (!empty($datasource))
        {
            foreach ($datasource as $row)
            {
                if ($row[$this->pidCOL] == $pid)
                {
                    $link = "<a href='" . $row[$this->urlCOL] . "' alt='" . $row[$this->describeCOL] .
                        "'>" . $row[$this->textCOL] . "</a>";
                    $ret = self::navmenu($datasource, $row[$this->idCOL]);
                    if (empty($ret))
                        $out .= "\n<li>$link</li>";
                    else
                        $out .= "\n<li>$link\n<ul>\n$ret\n</ul></li>";
                }
            }
        }
        if (!empty($out))
            $out = "\n<ul $this->css_class>$out\n</ul>";
        return $out;
    }

}

?>